;;; init.el -*- lexical-binding: t; -*-
;;
;; This file is part of Modulo.
;;
;; Modulo is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; Modulo is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Modulo.  If not, see <https://www.gnu.org/licenses/>.
;;
;;;

(defvar fmodula-gc-cons-threshold 16777216 ; 16MB
  "The default value to use for `gc-cons-threshold'. If you experience freezing,
decrease this value. If you experience stuttering or lag, increase this value.")

(defvar fmodula-gc-cons-upper-limit 268435456 ; 256MB
  "The temporary value for `gc-cons-threshold' to defer collection.")

(defvar fmodula--file-name-handler-alist file-name-handler-alist)

(defun fmodula|restore-startup-optimizations ()
  "This resets garbage collection settings to reasonable defaults. Please
note that a large `gc-cons-threshold' can cause random freezing. After the
`gc-cons-threshold' value is reset, `file-name-handler-alist' is also reset."
  (setq file-name-handler-alist fmodula--file-name-handler-alist)
  ;; Run this function on an idle time to defer a possible garbage collection
  ;; pause which could result; also allows deferred lazily-loaded packages to
  ;; take advantage of the garbage collection optimizations.
  (run-with-idle-timer
   3 nil (lambda ()
	   (setq-default gc-cons-threshold fmodula-gc-cons-threshold))))

(if (or after-init-time noninteractive)
    (setq gc-cons-threshold fmodula-gc-cons-threshold)
  ;; A big contributor to emacs-init-time is garbage collection.
  ;; This bit of code set a ceiling maximum temporary to prevent
  ;; collection from running, and then resets it later during
  ;; `fmodula|restore-startup-optimizations'.
  (setq gc-cons-threshold fmodula-gc-cons-upper-limit)
  ;; This is consulted on every `require', `load', and various path/IO functions.
  ;; Each iteration of this provides a minor speed boost.
  (setq file-name-handler-alist nil)
  ;; Not restoring these values to their defaults will cause freezing.
  (add-hook 'after-init-hook #'fmodula|restore-startup-optimizations))

;; Ensure that Modulo-fmodula is running out of this file's directory.
(setq user-emacs-directory (file-name-directory load-file-name))
;; In noninteractive sessions, prioritize non-bytecompiled source files to
;; prevent stale, bytecompiled code from running. However, if you are getting
;; recursive load errors, set this to nil.
(setq load-prefer-newer noninteractive)

;; Start the fmodula configuration.
(require 'core (concat user-emacs-directory "core/core"))
